/*
ID: pallab81
PROG:
LANG: C++
*/

//"I have not failed, I have just found 10000 ways that won't work.

#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <iomanip>

#include <ctime>
#include <cassert>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <cstdlib>

using namespace std;

#define VI vector< int >
#define CI( x ) scanf("%d",&x)
#define CL( x ) scanf("%lld",&x)
#define CD( x ) scanf("%lf",&x )
#define foR(i1,st,ed) for(int i1 = st,j1 = ed ; i1 < j1 ; ++i1 )
#define fo(i,ed) foR( i , 0 , ed )
#define foE(i,st,ed) foR( i , st , ed+1 )
#define foit(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define bip( X ) sleep( X )
#define Int long long
#define pb push_back
#define SZ(X) (int)(X).size()
#define LN(X) (int)(X).length()
#define mk make_pair
#define f first
#define s second
#define SET( ARRAY , VALUE ) memset( ARRAY , VALUE , sizeof(ARRAY) )

class ConvexSequence {
public:
    long long getMinimum(vector <int> a);
};
long long ConvexSequence :: getMinimum(vector <int> a) {

    Int RES=0LL;
    int n = SZ( a );
    while (true) {
        bool nochange=true;

        for (int i=1; i+1<n; ++i ) {
            if ( a[i-1]+a[i+1] >= (a[i]<<1) ) {
                continue;
            }
            int tobe = (a[i-1]+a[i+1])>>1;
            RES+= a[i]-tobe;
            a[i]=tobe;
            nochange=false;
        }
        if (nochange)
            break;
    }
    return RES;
}
// BEGIN KAWIGIEDIT TESTING
// Generated by KawigiEdit 2.1.8 (beta) modified by pivanof
#include <iostream>
#include <string>
#include <vector>
using namespace std;
bool KawigiEdit_RunTest(int testNum, vector <int> p0, bool hasAnswer, long long p1) {
    cout << "Test " << testNum << ": [" << "{";
    for (int i = 0; int(p0.size()) > i; ++i) {
        if (i > 0) {
            cout << ",";
        }
        cout << p0[i];
    }
    cout << "}";
    cout << "]" << endl;
    ConvexSequence *obj;
    long long answer;
    obj = new ConvexSequence();
    clock_t startTime = clock();
    answer = obj->getMinimum(p0);
    clock_t endTime = clock();
    delete obj;
    bool res;
    res = true;
    cout << "Time: " << double(endTime - startTime) / CLOCKS_PER_SEC << " seconds" << endl;
    if (hasAnswer) {
        cout << "Desired answer:" << endl;
        cout << "\t" << p1 << endl;
    }
    cout << "Your answer:" << endl;
    cout << "\t" << answer << endl;
    if (hasAnswer) {
        res = answer == p1;
    }
    if (!res) {
        cout << "DOESN'T MATCH!!!!" << endl;
    } else if (double(endTime - startTime) / CLOCKS_PER_SEC >= 2) {
        cout << "FAIL the timeout" << endl;
        res = false;
    } else if (hasAnswer) {
        cout << "Match :-)" << endl;
    } else {
        cout << "OK, but is it right?" << endl;
    }
    cout << "" << endl;
    return res;
}
int main() {
    bool all_right;
    all_right = true;

    vector <int> p0;
    long long p1;

    {
        // ----- test 0 -----
        int t0[] = {6,5,1,5,3,3};
        p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
        p1 = 7ll;
        all_right = KawigiEdit_RunTest(0, p0, true, p1) && all_right;
        // ------------------
    }

    {
        // ----- test 1 -----
        int t0[] = {3,0,1,4};
        p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
        p1 = 0ll;
        all_right = KawigiEdit_RunTest(1, p0, true, p1) && all_right;
        // ------------------
    }

    {
        // ----- test 2 -----
        int t0[] = {1,1,1,0,2,2,2};
        p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
        p1 = 5ll;
        all_right = KawigiEdit_RunTest(2, p0, true, p1) && all_right;
        // ------------------
    }

    {
        // ----- test 3 -----
        int t0[] = {854159326,317144183,781399725,287076509,894967145,882577367,174517516,134415519,274494874,709819883,59717133,732212854,40551288,232526958,811785438,930853743,946882902,321325300,397702677,376192501,599310562,889156198,135776890,882710939,823196361,681959076,318666702,94469186,536320456,116468376,530320850,436708006,903344748,659080120,774722507,967315412,566063635,43970906,497687103,781266213,876086123,366960001,587364849,191948103,172568553,539762057,83507466,71569625,686305822,663789601};
        p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
        p1 = 20178337330ll;
        all_right = KawigiEdit_RunTest(3, p0, true, p1) && all_right;
        // ------------------
    }

    {
        // ----- test 4 -----
        int t0[] = {5};
        p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
        p1 = 0ll;
        all_right = KawigiEdit_RunTest(4, p0, true, p1) && all_right;
        // ------------------
    }

    if (all_right) {
        cout << "You're a stud (at least on the example cases)!" << endl;
    } else {
        cout << "Some of the test cases had errors." << endl;
    }
    return 0;
}
// END KAWIGIEDIT TESTING
//Powered by KawigiEdit 2.1.8 (beta) modified by pivanof!
// kate: indent-mode cstyle; space-indent on; indent-width 0; 
